package com.xj.api.bussiness.device.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.device.service.ITbDeviceService;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.device.entity.TbDevice;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Mars
 * @since 2017-08-24
 */
@RestController
@RequestMapping("device")
public class TbDeviceController extends BaseController{
	
	@Autowired
	private ITbDeviceService deviceService;
	
	@GetMapping("/list.auth")
	@ApiOperation(value = "获取设备列表", httpMethod = "GET", notes = "根据用户电话，获取其下面的设备列表（分页）")
	public AbstractBean tbdeviceList(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize,@RequestParam String phone){
		return json(deviceService.selectMapsPage(new Page<TbDevice>(pageNum, pageSize), new EntityWrapper<TbDevice>().eq("owner", phone)));
	}
	
	@GetMapping("/{mac}.auth")
	@ApiOperation(value = "获取设备詳情", httpMethod = "GET", notes = "获取设备详情，参数为设备mac")
	public AbstractBean tbdeviceContent(@PathVariable Integer mac){
		return json(deviceService.selectById(mac));
	}
	
	@PostMapping("save.auth")
	@ApiOperation(value = "设备新增", httpMethod = "POST", notes = "设备新增")
	public AbstractBean add(TbDevice tbdevice){
		if(!deviceService.insertOrUpdate(tbdevice)){
		return error();
	    }
		return success();
	}
	@DeleteMapping("{id}/delete.auth")
	@ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
    public AbstractBean delete(@PathVariable(required=true) Integer id) {	
		if(!deviceService.deleteById(id)){
		return error();
		}
		return success();
    }
}
